package com.samsung.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.samsung.item.FileItem;
import com.samsung.sbackup.R;

public class FileAdapter2 extends BaseAdapter {

	Context context;
	public ArrayList<FileItem> files;

	public FileAdapter2(Context ctx) {
		this.context = ctx;
		files = new ArrayList<FileItem>();
	}

	public void addItem(FileItem f) {
		files.add(f);
		notifyDataSetChanged();
	}

	public void clear() {
		files.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public FileItem getItem(int arg0) {
		return files.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ViewHolder holder;
		if (arg1 == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			arg1 = inflater.inflate(R.layout.file_item_2, arg2, false);
			holder = new ViewHolder(arg1);
			arg1.setTag(holder);
		} else
			holder = (ViewHolder) arg1.getTag();

		holder.populateFrom(getItem(arg0));
		return arg1;
	}

	class ViewHolder {
		ImageView imgFile;
		TextView fileName;
		RadioButton cb;
		FileItem Fi;

		public ViewHolder(View row) {
			imgFile = (ImageView) row.findViewById(R.id.img_file_2);
			fileName = (TextView) row.findViewById(R.id.file_name_2);
			cb = (RadioButton) row.findViewById(R.id.cb);
		}

		public void populateFrom(FileItem f) {
			this.Fi = f;
			fileName.setText(f.fileName);
			cb.setChecked(f.isChecked());
			if (f.hasPassword())
			{
				imgFile.setImageResource(R.drawable.ic_img_file_lock);
			}
		}
	}
}
